package com.app.biz.controller.formal;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.common.annotation.Anonymous;
import com.app.common.core.page.TableDataInfo;
import com.app.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.formal.BizPowerConsumptionFormal;
import com.app.biz.service.IBizPowerConsumptionFormalService;


/**
 * 重点省份与全国工业用电量同比Controller
 * 
 * @author Zhou_S
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/biz/formal/consumption")
public class BizPowerConsumptionFormalController extends BaseController
{
    @Autowired
    private IBizPowerConsumptionFormalService bizPowerConsumptionFormalService;

    /**
     * 查询重点省份与全国工业用电量同比列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizPowerConsumptionFormal bizPowerConsumptionFormal)
    {
        if(StringUtils.isNotBlank(bizPowerConsumptionFormal.getDates())){
            DateTime datesTimes= DateUtil.parse(bizPowerConsumptionFormal.getDates(),"yyyy年MM月");
            bizPowerConsumptionFormal.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        }else{
            throw new RuntimeException("未指定日期！");
        }
        startPage();
        List<BizPowerConsumptionFormal> list = bizPowerConsumptionFormalService.selectBizPowerConsumptionFormalList(bizPowerConsumptionFormal);
        return getDataTable(list);
    }

    /**
     * 导出重点省份与全国工业用电量同比列表
     */
//    @PreAuthorize("@ss.hasPermi('consumption:formal:export')")
    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPowerConsumptionFormal bizPowerConsumptionFormal)
    {
        List<BizPowerConsumptionFormal> list = bizPowerConsumptionFormalService.selectBizPowerConsumptionFormalList(bizPowerConsumptionFormal);
        ExcelUtil<BizPowerConsumptionFormal> util = new ExcelUtil<BizPowerConsumptionFormal>(BizPowerConsumptionFormal.class);
        util.exportExcel(response, list, "重点省份与全国工业用电量同比数据");
    }

    /**
     * 获取重点省份与全国工业用电量同比详细信息
     */
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizPowerConsumptionFormalService.selectBizPowerConsumptionFormalByBaseId(baseId));
    }

    /**
     * 新增重点省份与全国工业用电量同比
     */
//    @PreAuthorize("@ss.hasPermi('consumption:formal:add')")
    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPowerConsumptionFormal bizPowerConsumptionFormal)
    {
        return toAjax(bizPowerConsumptionFormalService.insertBizPowerConsumptionFormal(bizPowerConsumptionFormal));
    }

    /**
     * 修改重点省份与全国工业用电量同比
     */
//    @PreAuthorize("@ss.hasPermi('consumption:formal:edit')")
    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPowerConsumptionFormal bizPowerConsumptionFormal)
    {
        return toAjax(bizPowerConsumptionFormalService.updateBizPowerConsumptionFormal(bizPowerConsumptionFormal));
    }

    /**
     * 删除重点省份与全国工业用电量同比
     */
//    @PreAuthorize("@ss.hasPermi('consumption:formal:remove')")
    @Log(title = "重点省份与全国工业用电量同比", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizPowerConsumptionFormalService.deleteBizPowerConsumptionFormalByBaseIds(baseIds));
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizPowerConsumptionFormalService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }
}
